package com.example.models;

import com.example.Utils.CommonStringUtils;
import com.example.Utils.DateUtils;
import io.ebean.ExpressionList;
import io.ebean.Finder;
import io.ebean.Model;
import lombok.Data;

import javax.persistence.*;
import java.util.Date;

@Entity
@Table(name = "user_basic")
@Data
public class User extends Model {
    @Id
    public Integer id;

    public String username;
    public String password;
    public String basicName;
    public Boolean isAlive = true;
    public Boolean isAdmin = false;
    public String headImgPath;
    public Date lastLogin;
    public String mobile;
    public Boolean isStu = true;
    public Date birthday;
    public String brief;
    public Date lastModifyTime;
    @ManyToOne
    @JoinColumn(name = "school_id")
    public Education school;

    @Enumerated(EnumType.ORDINAL)
    public Gender gender = Gender.MALE;

    public String province;
    public String city;
    public String area;

    public static enum Gender {
        MALE("男"), FEMALE("女");
        private String cname;
        Gender(String cname){
            this.cname = cname;
        }
        public String getCname(){
            return this.cname;
        }
    }

    public static Finder<Integer,User> find = new Finder<>(User.class);

    public static ExpressionList<User> search(){
        return User.find.query().where();
    }

    public String getPCA(){
        StringBuilder stringBuilder = new StringBuilder();
        if(!CommonStringUtils.isNullOrEmpty(province)){
            stringBuilder.append(province);
            if(!CommonStringUtils.isNullOrEmpty(city)){
                stringBuilder.append(" "+city);
                if(!CommonStringUtils.isNullOrEmpty(area)){
                    stringBuilder.append(" "+area);
                }
            }
        }
        return stringBuilder.toString();
    }

    public String  getBirthYear(){
        if(this.birthday == null){
            return "";
        }
        String birthStr = DateUtils.DateToStr(this.birthday,"yyyy-MM-dd");
        return birthStr.split("-")[0];
    }
    public String  getBirthMonth(){
        if(this.birthday == null){
            return "1";
        }
        String birthStr = DateUtils.DateToStr(this.birthday,"yyyy-MM-dd");
        return birthStr.split("-")[1];
    }
    public String  getBirthDay(){
        if(this.birthday == null){
            return "1";
        }
        String birthStr = DateUtils.DateToStr(this.birthday,"yyyy-MM-dd");
        return birthStr.split("-")[2];
    }

    public String getBirthdayStr(){
        return DateUtils.DateToStr(this.birthday,"yyyy-MM-dd");
    }
}
